﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class RoleManagement : System.Web.UI.Page
{
    Users user = new Users();
    Department dep = new Department();
    DataSet dsDep;
    DataSet dsUser;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["i"] = 0;
        if(Session["userName"]!=null)
        {
            if(!IsPostBack)
            {
                BindUsers();
            }
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl+"/Login.aspx");
        }

    }
    /// <summary>
    /// 绑定用户信息,并读取部门信息到dataSet中
    /// </summary>
    public void BindUsers()
    {
        dsDep = dep.FindDep();
        dgUsers.Columns[0].Visible=false;
        dsUser = user.ReadAllUsers(); 
        DataView dv = dsUser.Tables["userInfo"].DefaultView;        
        dgUsers.DataSource = dv;
        dgUsers.DataBind();
        
        
    }
    /// <summary>
    /// 显示所在部门和所属角色
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
     protected void dgUsers_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
     int i = (int)Session["i"];
     switch(e.Item.ItemType)
     {   
         case ListItemType.AlternatingItem:
         case ListItemType.Item:
            int  depID =Convert.ToInt32( dsUser.Tables["userInfo"].Rows[i][4]);
            int roleID = Convert.ToInt32(dsUser.Tables["userInfo"].Rows[i][5]);
            Label depName = (Label)e.Item.Cells[3].FindControl("lblDepName");
            Label roleName = (Label)e.Item.Cells[4].FindControl("lblRoleName");
            DataView dv = dsDep.Tables["department"].DefaultView;
            dv.RowFilter = "DEPID=" + depID;
            DataGrid1.DataSource = dv;
            DataGrid1.DataBind();
            depName.Text = DataGrid1.Items[0].Cells[1].Text; 
             switch(roleID)
             {
                 case 1:
                     roleName.Text = "系统管理员";
                     RadioButton man=(RadioButton)e.Item.Cells[5].FindControl("admin");
                     RadioButton funUser = (RadioButton)e.Item.Cells[5].FindControl("functionAdmin");
                     RadioButton user1 = (RadioButton)e.Item.Cells[5].FindControl("user");
                     Button btnSure=(Button)e.Item.Cells[6].FindControl("btnSure");
                     man.Checked = true;
                     if (Convert.ToString(dsUser.Tables["userInfo"].Rows[i][2]) == "管理员")
                     {
                         man.Enabled = false;
                         funUser.Enabled = false;
                         user1.Enabled = false;
                         btnSure.Enabled = false;
                     }
                     break;
                 case 2:
                     roleName.Text = "职能用户";
                     RadioButton funUser1 = (RadioButton)e.Item.Cells[5].FindControl("functionAdmin");
                     funUser1.Checked = true;
                     break;
                 case 3:
                     roleName.Text = "一般用户";
                      RadioButton user11=(RadioButton)e.Item.Cells[5].FindControl("user");
                      user11.Checked = true;
                     break;

             }
            Session["i"]=(int)Session["i"]+1;
            break;
     }
    
    }
    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void dgUsers_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgUsers.CurrentPageIndex = e.NewPageIndex;
        BindUsers();
    }
    /// <summary>
    /// 更改用户权限
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void dgUsers_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "sure")
        {
            string userName=e.Item.Cells[1].Text;
            int roleID = 0;
            RadioButton admin = (RadioButton)e.Item.Cells[5].FindControl("admin");
            RadioButton funUser = (RadioButton)e.Item.Cells[5].FindControl("functionAdmin");
            RadioButton user1 = (RadioButton)e.Item.Cells[5].FindControl("user");
            if(admin.Checked)
            {
                roleID = 1;
            }
            if(funUser.Checked)
            {
                roleID = 2;
            }
            if(user1.Checked)
            {
                roleID = 3;
            }
            if (user.UpdateRole(userName, roleID))
            {
                BindUsers();
                Response.Write("<script language='javascript'>alert('修改成功！')</script>");
            }
            else 
            {
                Response.Write("<script language='javascript'>alert('在尝试修改角色时，发生了未知错误，修改失败！')</script>");
            }
        }
    }
}
